//
//  WYEmoticonToorBar.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/25.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit

//MARK: -- 1 按钮的枚举类型
enum WYEmoticonToolBarButtonType: Int {
    //枚举的原始值 0 做枚举视图的tag 值需要注意  0 取得是当前自己
    //默认的
    case Normal = 1000
    //Emoji
    case Emoji = 1001
    //浪小花
    case Lxh = 1002
    
}

class WYEmoticonToorBarView: UIStackView {
    
    //MARK: --4  定义 Button 闭包  用于和外界联系的接口
    var selectedButtonClosure: ((type: WYEmoticonToolBarButtonType) -> ())?
    
    //记录选中按钮
    var lastSelectedButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        
        //设置布局方式
        axis = .Horizontal
        
        //布局填充
        distribution = .FillEqually
        
        
        addChildButton("compose_emotion_table_left", title: "默认", type: .Normal)
        addChildButton("compose_emotion_table_mid", title: "Emoji", type: .Emoji)
        addChildButton("compose_emotion_table_right", title: "浪小花", type: .Lxh)
        
    }
    //MARK: -- 2-创建子按钮 添加一个枚举类型的参数 用于区别
    private func addChildButton(imageName: String?, title: String?,type: WYEmoticonToolBarButtonType) {
        
     
        
        //设置文字
        let button = UIButton()
        //MARK: -- 3 .设置 tag 值为枚举值  获取tag 值  将 枚举值 给 tag 值
        button.tag = type.rawValue
        
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: imageName! + "_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: imageName! + "_selected"), forState: .Selected)
        
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        //添加点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        addArrangedSubview(button)
        
        //默认选中
        if type == .Normal {
        
            lastSelectedButton?.selected = false
            button.selected = true
            lastSelectedButton = button
        }
    }
    
    //点击事件
    @objc private func buttonAction(button: UIButton) {
        
        //判断是否为同一个按钮
        if lastSelectedButton == button {
        
            return
        }
        //只选中一个
        lastSelectedButton?.selected = false
        button.selected = true
        lastSelectedButton = button
        
        // MARK: --7 通过 tag 获取枚举成员 执行闭包回调
        let type = WYEmoticonToolBarButtonType(rawValue: button.tag)!
        
        selectedButtonClosure?(type: type)
    }
    
    //根据外界传入的 section 选中响应的按钮 这个就是顺掉
    func selectedButtonwithSection(section: Int) {
        
        //通过外界的 section 作为 tag 获取按钮
        let button = viewWithTag(section + 1000) as! UIButton
        print(button)
        
        if lastSelectedButton == button {
            
            return 
        }
        
        lastSelectedButton?.selected = false
        button.selected = true
        lastSelectedButton = button
    }
}
